// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// R [author] rhyme_lph
// P [email]  rhymelph@gmail.com
// G [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/grant_entity.dart';
import 'package:finance_app/src/mixin/format_group_map_mixin.dart';
import 'package:finance_app/src/mixin/search_data_list_mixin.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/page/dashboard/cash_in/select_grant_page.dart';

class SelectGrantProvider extends BaseProvider
    with SearchDataListMixin, FormatGroupMapLocalMixin<GrantEntity> {
  GrantEntity currentSelect;

  @override
  Future get dataFuture => null;

  @override
  void initState() {
    super.initState();
    contactToMap();
  }

  @override
  void searchRefreshData(searchData) {
    contactToMap();
    update();
  }

  void contactToMap() {
    groupMap.clear();
    sortGroupToMap(Config.settingData.grants,
        group: (a) => '',
        filter: (a) => onFilterSearch(searchController.text, a.name));
  }

  void onSelectGrant(GrantEntity value) {
    currentSelect = value;
    update();
    obtainContext?.call((context) {
      Navigator.of(context).pop(value);
    });
  }
}
